package cn.tangwei.basic.service.impl;

import cn.tangwei.basic.mapper.BaseMapper;
import cn.tangwei.basic.query.BaseQuery;
import cn.tangwei.basic.service.IBaseService;
import cn.tangwei.basic.utils.PageList;
import cn.tangwei.org.domain.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 抽取公共的service层实现类
 *   基本的CRUD 分页加高级查询 批量删除
 * @param <T>
 */

@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {
    @Autowired
    private BaseMapper<T> baseMapper;
    @Override
    @Transactional
    public void add(T t) {
        baseMapper.save(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        baseMapper.remove(id);
    }

    @Override
    public T queryById(Long id) {
        return baseMapper.queryById(id);
    }

    @Override
    public List<T> queryAll() {
        return baseMapper.queryAll();
    }

    @Override
    public PageList<T> queryPage(BaseQuery query) {
        Integer total = baseMapper.queryCount(query);
        List<T> rows = baseMapper.queryData(query);
        return new PageList<>(total, rows);
    }

    @Override
    @Transactional
    public void patchDelete(List<Long> ids) {
        baseMapper.patchRemove(ids);
    }
}
